package com.gaohe.controller;

import com.gaohe.common.MinioUtils;
import com.gaohe.common.Result;
import com.gaohe.config.MinioConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioUtils minioUtils;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("/common:upload,参数：{}",file.getOriginalFilename());
//        //读取本地文件
//        FileInputStream is = new FileInputStream("D:\\images\\paidaxing.jpg");
//        //定义文件名称
//        //1.时间戳  2.UUID
//        String name = UUID.randomUUID().toString();
//        String fileName = name + ".jpg";
//        //上传到minio
//       minioUtils.uploadFile(minioConfig.getBucketName(),fileName,is);
        //1.获取文件原名称   后缀名
        String originalFilename = file.getOriginalFilename();
        //2.重新命名
        String UUIDName = UUID.randomUUID().toString();
        //   xx-xx.png
        String type = StringUtils.substringAfterLast(originalFilename, ".");
        String fileName = UUIDName + "." + type;
        //3.存入桶
        minioUtils.uploadFile(
                minioConfig.getBucketName(), file,fileName,file.getContentType()
                );
        //4.返回前端文件名称
//        Object o  = fileName;
        return Result.success(fileName,"");

    }
}
